﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 功能列表
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.20 
    /// </summary>
    public class ModuleColumnBll
    {
        private ModuleColumnIService service = new ModuleColumnService();

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<ModuleColumnEntity>> GetList()
        {
            try
            {
                return await service.GetList();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="moduleId">功能主键</param>
        /// <returns></returns>
        public async Task<List<ModuleColumnEntity>> GetList(string moduleId)
        {
            try
            {
                return await service.GetList(moduleId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<ModuleColumnEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(ModuleColumnEntity entity)
        {
            try
            {
                return await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task<int> Create(ModuleColumnEntity entity)
        {
            try
            {
                var bindTableList = await service.GetListByBindTable(entity.BindTable);
                if (bindTableList.Count > 0)
                {
                    entity.SortCode = bindTableList.FindLast(q => q.BindTable == entity.BindTable).SortCode + 1;
                }
                else
                {
                    entity.SortCode = CommonHelper.SortCode;
                }
                entity.Id = CommonHelper.Guid;
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entitys">实体对象</param>
        public async Task<int> Create(List<ModuleColumnEntity> entitys)
        {
            try
            {
                var sortCode = CommonHelper.SortCode;
                var userId = UserProvider.Instance.Get().UserId;
                foreach (var entity in entitys)
                {
                    entity.Id = CommonHelper.Guid;
                    entity.EnabledMark = 1;
                    entity.SortCode = sortCode++;
                    entity.CreatorTime = DateTime.Now;
                    entity.CreatorUserId = userId;
                }
                return await service.Create(entitys);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        public async Task<int> Update(string id, ModuleColumnEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

    }
}
